//
//  ViewController.swift
//  SFQuestion
//
//  Created by sr liu on 2021/6/22.
//

import UIKit



func suiyi() {
    let a: Array = [1]
}




class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .cyan
        
//        let hot = SolutionHot()
//        hot.twoSum2([3,2,4], 6)
        
        //直接操作字符串
        let s = "abcd"
        var c: Character? = nil
        for (i,character) in s.enumerated() {
            print("\(i)  \(character)")
            c = character
        }
        
//        testOther()
    }
    
    func old() {
        //        let n = -1 crash
        //        for i in 0..<n {
        //
        //        }
                
        //        testStack()
        //        testSort()
        //        testBit()
//                testOther()
//                testLec()
                return
                
                let a = false
                let b = true
                let c = false
                if a || b && c {
                    print("----")
                }
                
                
                let str = "Swift"
                let arrr = Array(str.unicodeScalars)
                print(arrr)
                
                print(replaceStr(str, 1, "oc"))//Socift
                print(str)
                print(replaceStr(str, 1, "o"))//Soift
                print(replaceStr(str, 4, "o"))//Swifo
                print(replaceStr(str, 5, "o"))
                
                
                for _ in 1..<1 {
                    print("-----")
                }
                
                
                var arrIn = [1,2,3]
                var arrOut = Array<Array<Int>>.init()
                arrOut.append(arrIn)
                arrIn.removeLast()
                print(arrOut)
                
                
        //        let s = "Swift"
        //        let i = s.index(s.startIndex, offsetBy: 4)
        //        print(s[i])
                
        //        let numbers = [10, 20, 30, 40, 50]
        //        print(numbers.endIndex)//5
        //        let s = "abcd\0 ef"
        //        let c = Character("\0")
        //        for i in s {
        //            if i == c {
        //                print(i)
        //            } else {
        //                print("1")
        //            }
        //        }
        //        print(c)
        //        print(s)
        //        print(s.count)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        print("\(#function)")
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        
        var node: TreeNode? = nil
        var node1: TreeNode? = nil
        var v = node1?.val
        // 这是node1?.val  (Int?)  如果两个都为nil就相等了
        if node1?.val == node?.val {
            print("---")
        }
        
//        treeTest()
        
//        DPTest()
//        let offer = Solution()
//        print(offer.numWays(1))
//        print(offer.numWays2(7))
//        print(offer.fibonacci2(45))
//        let arr = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]]
//        print(offer.findNumberIn2DArray([], 0))
        
//print(offer.replaceSpace("We are happy."))
//        offer.buildTree([3,9,20,15,7], [9,3,15,20,7])
//        creatTreeTest()
        
        
        
//        test12()
//        testListNode2()
        
        
//        letTest()
//        hotTest()
        hotPlanTest()
        
        
        
//        var arr = [Int]()
//        arr.append(1)
//        arr.append(2)
//        
//        print(arr[0])
        
//        let n = SolutionDPProblem()
//        
//        print(n.lengthOfLongestSubstring1("abcabcbb"))
    }
    
    func letTest() {
        let code = LetCode()
//        print(code.addStrings("9934", "78"))
        
    }
    
    func DPTest() {
        let dp = DP()
//        print(dp.mem)
//        dp.testDp()
        print(dp.money(7))
    }
    
    func creatTreeTest() {
        let tree = createBiTree([5,7,6,8,4,-1,9])
        preOrderRecursion(tree)
        print("----")
        preOrderNoRecursion(tree)
    }
    
    func testSort() {
        let sortObj = Sort()
//        var arr = [5,1,2,3,6,4,7,8,9]
//        sortObj.QuickSort(&arr)
//        sortObj.BubbleSort2(&arr)
//        print(arr)
        
        var arr = [2,4,1,0,3,5]
        
        var a = [0,0,2,3,2,1,1,2,0,4]
        sortObj.QuickSort(&a)
//        print(SortProblem().getLeastNumbers([0,0,2,3,2,1,1,2,0,4],10))
//
//        print(SortProblem().GetNumberOfK([1,2,3,4,5], 3))
//
//        print(SortProblem().findNthDigit(1))
        
        print(SortProblem().Permutation("ab"))
        
    }
    
    func test12() {
//        [["C","A","A"],["A","A","A"],["B","C","D"]]
//        "AAB"
//        let s = "AAB"
//        let board:[[Character]] = [["C","A","A"],["A","A","A"],["B","C","D"]]
        
        
        let offer = Solution()
//        print(offer.exist(board, s))
//        print(offer.movingCount(3, 3, 6))
//        print(offer.cuttingRope(10))
//        print(offer.cuttingRope(2))
//        print(offer.myPow1(2, -1))
//        print(offer.myPow1(2, -1))
//        print(offer.printNumbers(1))
//        offer.printN(3)
//        offer.printN2(3)
//        print(offer.isNumber("0"))
//        print(offer.isNumber("3.14e1-"))
//        print(offer.isNumber("3.14e1"))
//        print(offer.isNumber(".14E1"))
//        print(offer.isNumber("3."))
//        print(offer.isNumber("r3."))
//        print(offer.isNumber("3e."))
//        print(offer.exchange([1,2,3,4]))
//        offer.getKthFromEnd(ListNode.init(2), 0)
//        offer.validateStackSequences([1,2,3,4,5], [4,5,3,2,1])
//        print(offer.maxSubArray([-2,1,-3,4,-1,2,1,-5,4]))
        print(offer.reverseWords(" abc. ef. "))
        print(offer.reverseLeftWords("abc1234", 1))
    }
    
    
    func testListNode() {
        let node1 = ListNode.init(4)
        let node2 = ListNode.init(5)
        let node3 = ListNode.init(1)
        let node4 = ListNode.init(9)
        node1.next = node2
        node2.next = node3
        node3.next = node4
        
        let offer = Solution()
        offer.deleteNode(node1, 1)
        
        
    }
    
    func testListNode1() {
        let node1 = ListNode.init(1)
        let node2 = ListNode.init(2)
        let node3 = ListNode.init(3)
        let node4 = ListNode.init(4)
        let node5 = ListNode.init(5)
        let node6 = ListNode.init(6)
        node1.next = node2
        node2.next = node3
        node3.next = node4
        node4.next = node5
        node5.next = node6
        node6.next = node3
        
        let offer = Solution()
        offer.detectCycle(node1)
        
        
    }
    
    func testListNode2() {
        let node1 = ListNode.init(1)
        let node2 = ListNode.init(2)
        let node3 = ListNode.init(3)
        let node4 = ListNode.init(4)
        let node5 = ListNode.init(5)
        node1.next = node2
        node2.next = node3
        node3.next = node4
        node4.next = node5
        
        let offer = Solution()
        offer.reverseList(node1)
        
        
    }
    
    func treeTest() {
        let offer = TreeSolution()
        
        let tree = createBiTree([5,4,8,11,-1,13,4,7,2,-1,-1,-1,-1,5,1])
        
        print(offer.pathSum(tree, 22))
//        offer.verifyPostorder([4, 8, 6, 12, 16, 14, 10])
//        offer.verifyPostorder([1,2,5,10,6,9,4,3])
        
        let niuke = NiuKe()
        let biTree = createBiTree([5,3,7,2,4,6,8])
//        print(niuke.KthNode(biTree, 3))
        
        
        let tree1 = createBiTree([10,5,12,4,7,-1,-1])
        print(niuke.FindPath(tree1, 22))
    }
    
    func testDp() {
        let p = SolutionDPProblem()
//        p.Fibonacci(4)
//        p.Fibonacci2(4)
//        print(p.lengthOfLongestSubstring("Kzz8"))
        p.translateNum(26)
    }
    
    func testStack() {
        let p = SolutionStack()
        print(p.solution_3("  hello world!  "))
        print(p.ReverseSentence2("  hello world!  "))
        print(p.ReverseSentence2(" "))
    }
    
    func testBit() {
        BitOp().NumberOf1(-2147483648)
    }
    
    func testOther() {
//        other().FirstNotRepeatingChar("google")]
        print(other().printNumbers(3))
        let oth = other()
//        oth.Insert("g")
//        oth.Insert("o")
//        oth.Insert("o")
//        oth.Insert("g")
//        oth.Insert("l")
//        oth.Insert("e")
//        oth.FirstAppearingOnce()
    }
    
    func hotTest() {
        let s = SolutionHot()
//        s.lengthOfLIS([10,9,2,5,3,7,101,18])
//        s.maxSubArray([-2,1,-3,4,-1,2,1,-5,4])
//        let tree = createBiTree([1,-1,2,-1,-1,3,-1])
//        s.inorderTraversal(tree)
//        s.findMin([1,3])
//        s.isValid("()")
        
//        print(s.decodeString("3[a]2[bc]"))
//        print(s.decodeString("3[a2[c]]"))
//        print(s.dailyTemperatures([73,74,75,71,69,72,76,73]))
//        print(s.climbStairs(1))
//        print(s.coinChange([1], 0))
//        s.wordBreak("leetcode", ["leet","code"])
//        s.canPartition([1,5,11,5])
//        s.groupAnagrams1(["eat","tea","tan","ate","nat","bat"])
    }
    
    
    
    func testLec() {
        let lru = LRUCache(2)
        print(lru.get(2))
        lru.put(2, 6)
        print(lru.get(1))
        lru.put(1, 5)
        
        lru.put(1, 2)
        
        print(lru.get(1))
        print(lru.get(2))
    }
    
    func hotPlanTest() {
        let hot = PlanHot100()
        hot.jump([2,3,1,2,4,2,3])
        
    }
    
}

